package de.bbl.android.fragment.DataCenter.Spielplan;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import de.bbl.android.BBLAndroidFragmentActivity;
import de.bbl.android.R;
import de.bbl.android.adapter.GameCenter.GameCenterArrayAdapter;
import de.bbl.android.classes.Match;
import de.bbl.android.tools.Tools;
import de.bbl.android.tools.XmlParseGameDayTools;

public class DataCenterSpielplanSpieltagFragment extends ListFragment {

	private Activity activity;
	private Match[] currentLoaded;
	private boolean finished = true;
	private String matchDay = "1";
	private String competitionId = "1";

	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        activity = getActivity();
        if (savedInstanceState == null && currentLoaded == null) {
        	RefreshData();
        }
    }
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	public boolean isFinished() {
		return finished;
	}

	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public void setMatchDay(String match) {
		matchDay = match;
	}
	
	public void RefreshData() {
        setHasOptionsMenu(false);
        if (activity != null) {
    		startProgress(activity.getCurrentFocus());
        }
    }

	private void loadData() {
		String[] dataArrayUrl = getResources().getStringArray(R.array.Data_Files_Url);
		String[] dataArray = getResources().getStringArray(R.array.Data_Files);
		String dataUrl = "";
		String data = "";
		dataUrl = dataArrayUrl[BBLAndroidFragmentActivity.DATA_MATCHDAYS] + "/" + competitionId + "/" + matchDay;
		data = dataArray[BBLAndroidFragmentActivity.DATA_MATCHDAYS] + competitionId + "_" + matchDay;
		Tools.getGamedayData(getResources().getString(R.string.main_url), dataUrl, data, BBLAndroidFragmentActivity.bbl_temp_sd_path);
		currentLoaded = XmlParseGameDayTools.XmlParseGameDay(BBLAndroidFragmentActivity.bbl_temp_sd_path + data);
	}
    
    private void startProgress(final View view) {
    	final Handler refresh = new Handler(Looper.getMainLooper());
    	Runnable runnable = new Runnable() {			
			public void run() {
				setFinished(false);
				loadData();
		    	refresh.post(new Runnable() {
					public void run() {
						if (! isDetached()) {							
							GameCenterArrayAdapter gcaa = new GameCenterArrayAdapter(activity, currentLoaded);
							setListAdapter(gcaa);
						}
					}
				});				
			}
		};		
		new Thread(runnable).start();
    }

	public String getCompetitionId() {
		return competitionId;
	}

	public void setCompetitionId(String competitionId) {
		this.competitionId = competitionId;
	}
}
